package com.wang.tool.executor.transitivethreadcontext;

import java.util.concurrent.Executor;

/**
 * 自切换Executor
 *
 * @Author shaopeng
 * @Date 2021/11/13
 */
public class TransitiveExecutor implements Executor {
    /**
     * 代理 非空
     */
    private final Executor delegate;

    public TransitiveExecutor(Executor delegate) {
        if (delegate == null) {
            throw new IllegalArgumentException("delegateExecutor can not be null");
        }
        this.delegate = delegate;
    }

    /**
     * 提交任务
     */
    @Override
    public void execute(Runnable command) {
        delegate.execute(ThreadContextTransferTool.wrap(command));
    }

    public Executor getDelegate() {
        return delegate;
    }

    @Override
    public boolean equals(Object o) {
        return delegate.equals(o);
    }

    @Override
    public int hashCode() {
        return delegate.hashCode();
    }

    @Override
    public String toString() {
        return getClass().getName() + "[" + delegate.toString() + "]";
    }
}
